extends Chara

var baseId = ""
var charaInfo 
var killNum = 0 #杀敌数
var inited = false
var Utils = globalData.infoDs["g_ZHPUtils"]
var ZHPData = preload("./charaData.gd").getCharaData()

func _init():
	._init()

func _extInit():
	._extInit()
	chaName = ZHPData.name
	attCoe.atkRan = 10                #攻击距离
	attCoe.maxHp = 10                 #最大生命（呈现在游戏中时，每点代表112.5，取整数，下同）
	attCoe.atk = 10                  #攻击力（每点代表8.5）
	attCoe.mgiAtk = 10                #魔法攻击（每点代表13.7）
	attCoe.def = 10                   #物理防御（每点代表15）
	attCoe.mgiDef = 10                #魔法防御（每点代表16.6）
	lv = 4                            #等级的设置
	atkEff = "sk_feiDang"                #攻击时的特效
	attAdd.cri += 5
	attAdd.criR += 5
	attAdd.spd += 5
	attAdd.dod += 5
	attAdd.mgiAtkL += 5
	attAdd.atkL += 5
	attAdd.defL += 5
	attAdd.mgiDefL += 5
	attAdd.maxHpL += 5
	attAdd.reHp += 5
	attAdd.suck += 5
	attAdd.mgiSuck += 5
	attAdd.atkR += 5
	attAdd.defR += 5
	attAdd.penL += 5
	attAdd.mgiPenL += 5
	attAdd.cd += 5
	addSkillTxt(ZHPData.SKILL1_TEXT_1)
	addSkillTxt(ZHPData.SKILL1_TEXT_2)
	addSkillTxt(ZHPData.SKILL1_TEXT_3)
	addSkillTxt(ZHPData.SKILL1_TEXT_4)
	if not inited:
		inited = true
		if not is_connected("onPressed", self, "chaInfo"): 
			connect("onPressed",self,"chaInfo")
		if not is_connected("onAddItem", self, "onAddItem"):
			connect("onAddItem",self,"onAddItem")
		sys.main.connect("onBattleStart",self,"Start")
		sys.main.connect("onBattleEnd",self,"End")
		sys.main.player.connect("onAddCha", self, "AddCha")
		sys.main.player.connect("onAddItem", self, "AddItem")

func _connect():
	._connect()

func Start():
	for i in sys.main.btGrid.get_children():
		var ee:Chara = i.get_child(0)
		if ee.id != "cex___E-Hide" && ee != null:
			sys.main.btGrid.remove_child(ee)
	for k in sys.main.itemGrid.get_children():
		sys.main.itemGrid.remove_child(k)
	clearAll()
	zhangwo()

func End():
	for i in sys.main.btGrid.get_children():
		var ee:Chara = i.get_child(0)
		if ee.id != "cex___E-Hide" && ee != null:
			sys.main.btGrid.remove_child(ee)
	for k in sys.main.itemGrid.get_children():
		sys.main.itemGrid.remove_child(k)

func AddCha(cha):
	if cha != null && cha.get("tag") != "E-Hide":
		cha.free()

func AddItem(item):
	if item != null:
		sys.main.player.delItem(item)

func _onAtkChara(atkInfo:AtkInfo):
	._onAtkChara(atkInfo)
	if atkInfo.atkType == AtkType.NORMAL:
		hurtChara(atkInfo.hitCha,att.mgiAtk,Chara.HurtType.REAL,Chara.AtkType.EFF)

func _onAddBuff(buff:Buff):
	._onAddBuff(buff)
	buff.isDel = true

func onAddItem(item):
	yield(sys.get_tree().create_timer(0.05), "timeout")
	delItem(item)

func _onDeath(atkInfo):
	._onDeath(atkInfo)
	isDeath = false
	plusHp(att.maxHp,false)

func _onHurt(atkInfo:AtkInfo):
	._onHurt(atkInfo)
	if atkInfo.atkType == Chara.AtkType.SKILL and atkInfo.atkCha != self:
		if sys.rndPer(self.att.dod*100):
			atkInfo.isMiss = true
	if atkInfo.atkType == Chara.AtkType.EFF and atkInfo.atkCha != self:
		if sys.rndPer(self.att.dod*100):
			atkInfo.isMiss = true
	if atkInfo.atkCha == null or atkInfo.atkCha.isDeath or atkInfo.hurtVal == 0:
		atkInfo.hurtVal = 0

func _upS():
	._upS()
	plusHp(att.maxHp*0.01,true)
	zhangwo()

func _onKillChara(atkInfo:AtkInfo):
	._onKillChara(atkInfo)
	killNum += 1

func zhangwo():
	var chas = getAllChas(1)
	for i in chas:
		if i != null:
			i.att.hp = -1000000
			hurtChara(i,0,Chara.HurtType.REAL,Chara.AtkType.EFF)

func clearAll():
	for i in Utils.getAllCells(1):
		var cha = sys.main.matCha(i)
		if cha != null && cha.team != 2 && cha.id != "cex___E-Hide": 
			sys.main.delMatChara(cha)

#人物信息附加
func chaInfo(cha):
	yield(cha.reTimer(0.05),"timeout")
	charaInfo = sys.get_node("/root/topUi/charaInfoMsg")
	var txt = charaInfo.get_node("txt")
	var split = txt.bbcode_text.rsplit("\n")
	var res = ""
	for i in range(split.size()):
		if i == 0:
			res += split[i] + "            [color=#FFA500]"+tr("g_kill")+"：[/color]{0}\n".format({"0":cha.killNum})
		elif i == 1:
			res += "[color=#66AB5F]"+tr("g_hp")+"：[color=#FFA500]{0}[/color]/{1}".format({"0":int(cha.att.hp),"1":int(cha.att.maxHp)})
			res += "  "+tr("g_dod")+"：[color=#FFA500]{2}%[/color]".format({"2":int(cha.att.dod*100)})
			res += "  "+tr("g_spd")+"：[color=#FFA500]{3}%[/color]".format({"3":int(cha.att.spd*100)})
			res += "  "+tr("g_cd")+"：[color=#FFA500]{4}%[/color]\n".format({"4":int(cha.att.cd*100)})
		elif i == 2:
			res += ""+tr("g_atkRan")+"：[color=#FFA500]{0}[/color]".format({"0":cha.att.atkRan})
			res += "  "+tr("g_cri")+"：[color=#FFA500]{1}%[/color]".format({"1":int(cha.att.cri*100)})
			res += "  "+tr("g_criR")+"：[color=#FFA500]{2}%[/color]\n".format({"2":int((cha.att.criR+2)*100)})
		elif i == 3:
			res += ""+tr("g_atk")+"：{0}([color=#FFA500]{1}%[/color])".format({"0":int(cha.att.atk),"1":int(cha.att.atkL*100)})
			res += "  "+tr("g_mgiAtk")+"：{2}([color=#FFA500]{3}%[/color])\n".format({"2":int(cha.att.mgiAtk),"3":int(cha.att.mgiAtkL*100)})
		elif i == 4:
			var d1 = int(cha.att.def / (100 + cha.att.def) * 100)
			var d2 = int(cha.att.mgiDef / (100 + cha.att.mgiDef) * 100)
			var d3 = int(10000.0/max(1,cha.att.cd*100+100))
			res += ""+tr("g_def")+"：{0}([color=#FFA500]{1}%[/color]|{2}%)".format({"0":int(cha.att.def),"1":int(cha.att.defL*100),"2":d1})
			res += "  "+tr("g_mgiDef")+"：{0}([color=#FFA500]{1}%[/color]|{2}%)\n".format({"0":int(cha.att.mgiDef),"1":int(cha.att.mgiDefL*100),"2":d2})
			res += ""+tr("g_suckpen")+"：[color=#FFA500]{0}%[/color]|[color=#FFA500]{1}[/color]|[color=#FFA500]{2}%[/color]\n".format({"0":int(cha.att.suck*100),"1":int(cha.att.pen),"2":int(cha.att.penL*100)})
			res += ""+tr("g_mgiSuckpen")+"：[color=#FFA500]{0}%[/color]|[color=#FFA500]{1}[/color]|[color=#FFA500]{2}%[/color]\n".format({"0":int(cha.att.mgiSuck*100),"1":int(cha.att.mgiPen),"2":int(cha.att.mgiPenL*100)})
			res += ""+tr("g_atkRdefR")+"：[color=#FFA500]{0}%[/color]|[color=#FFA500]{1}%[/color]".format({"0":int(cha.att.atkR*100),"1":int(cha.att.defR*100)})
			res += "  "+tr("g_heal")+"：[color=#FFA500]{0}%[/color]|[color=#FFA500]{1}%[/color]\n".format({"0":int(cha.att.maxHpL*100),"1":int(cha.att.reHp*100)})	
			# res += "实际技能用时[color=#FFA500]{0}%[/color]\n".format({"0":d3})
		else:
			res += split[i] + "\n"
	yield(cha.reTimer(0.10),"timeout")
	txt.bbcode_text = res
	charaInfo.get_node("Button").hide()